package org.ccay.security.dimension.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.ccay.core.base.KeyValuePair;
import org.ccay.core.base.PageVO;
import org.ccay.core.base.PagedResult;
import org.ccay.security.dao.IRoleDimensionDataProviderDao;
import org.ccay.security.dimension.DimensionDataProvider;
import org.ccay.security.entity.Role;
import org.ccay.security.vo.RoleVO;

/**
 * 角色维度数据提供者
 * @author chaowangbang
 *
 */
@Named("SYS_ROLE_PROVIDER")
public class RoleDimensionDataProvider implements DimensionDataProvider{
	
	@Inject
	private IRoleDimensionDataProviderDao roleDao;
	
	@Override
	public PagedResult<KeyValuePair<? extends Serializable, String>> findPagedDimensionValues(
			KeyValuePair<? extends Serializable, String> condition, PageVO page) {
		//转换查询条件
		RoleVO roleCondition = new RoleVO();
		if(null != condition.getKey()){
			roleCondition.setId((Long)condition.getKey());
		}
		if(null != condition.getValue()){
			roleCondition.setName(condition.getValue());
		}
		PagedResult<RoleVO> pagedRoles = roleDao.findPagedRoles(roleCondition,page);
		//转换返回结果
		PagedResult<KeyValuePair<? extends Serializable, String>> resultPage = new PagedResult<KeyValuePair<? extends Serializable,String>>();
		List<KeyValuePair<? extends Serializable,String>> resultList = new ArrayList<KeyValuePair<? extends Serializable,String>>();
		for(RoleVO role : pagedRoles.getResult()){
			resultList.add(new KeyValuePair<Long,String>(role.getId(),role.getName()));
		}
		resultPage.setPageVO(pagedRoles.getPageVO());
		resultPage.setResult(resultList);
		
		return resultPage;
	}
	
	@Override
	public List<KeyValuePair<? extends Serializable, String>> findDimensionDisplayValues(List<? extends Serializable> keys) {
		//转换查询条件
		@SuppressWarnings("unchecked")
		List<Long> ids = (List<Long>) keys;
		List<RoleVO> roles = roleDao.findRoleByIds(ids);
		//转换返回结果
		List<KeyValuePair<? extends Serializable,String>> resultList = new ArrayList<KeyValuePair<? extends Serializable,String>>();
		for(Role role : roles){
			resultList.add(new KeyValuePair<Long,String>(role.getId(),role.getName()));
		}
		return resultList;
	}

}
